package com.example.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
	private final static int DB_VERSION = 1;
	private final static String DB_NAME = "peopleInfo.db";
	private final static String TABLE_NAME = "people";

	public DBHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table " + TABLE_NAME+ "( _id integer primary key autoincrement,name text,pwd text)");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
	}

	public void insert(String name, String pwd) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.execSQL("insert into " + TABLE_NAME + "(name,pwd) values(?,?)",
				new String[] { name, pwd });
	}

	public void update(int id, String text1, String text2) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put("name", text1);
		cv.put("pwd", text2);
		db.update(TABLE_NAME, cv, "_id= ?",
				new String[] { Integer.toString(id) });
	}

	public Cursor select() {
		SQLiteDatabase db = this.getWritableDatabase();
		// Cursor c=db.rawQuery("select * from "+TABLE_NAME, null);
		Cursor cursor = db
				.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}


}
